//
//  ViewController.m
//  UI11_Model的使用
//
//  Created by Scott on 16/3/23.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "ViewController.h"
#import "TableViewOfContact.h"
#import "ModelOfContact.h"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) TableViewOfContact *tableView;


@property (nonatomic, retain) NSMutableDictionary *dicOfNew;

@property (nonatomic, retain) NSArray *arrOfkeys;

@end

@implementation ViewController

- (void)dealloc {
    
    [_tableView release];
    [_dicOfNew release];
    [_arrOfkeys release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self handleData];
    
    [self handleJSON];
    
    [self createTableView];
    
}

#pragma mark - Data
- (void)handleData {
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160101" ofType:@"plist"];
    
    NSDictionary *dicOfSource = [NSDictionary dictionaryWithContentsOfFile:path];
    
    // 新字典初始化.
    self.dicOfNew = [NSMutableDictionary dictionary];
    
    // 构建新字典, 里面含有model
    for (NSString *key in dicOfSource) {
        
        NSArray *arrOfContact = [dicOfSource objectForKey:key];
        
        // 创建新数组,用于存放model对象.
        NSMutableArray *arrOfNew = [NSMutableArray array];
        
        // 遍历数组
        for (NSDictionary *dic in arrOfContact) {
            
            ModelOfContact *model = [[ModelOfContact alloc] init];
            
            // model 赋值(KVC)
            [model setValuesForKeysWithDictionary:dic];
            
            // model 放入新数组中
            [arrOfNew addObject:model];
            [model release];
        }
        
        // 将存放model的数组放入新字典中.
        [self.dicOfNew setObject:arrOfNew forKey:key];
    }
    
    self.arrOfkeys = [[self.dicOfNew allKeys] sortedArrayUsingSelector:@selector(compare:)];

    
    
}

- (void)handleJSON {
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"news" ofType:@"txt"];
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    // 核心API:
    id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    NSLog(@"%@", result);
    
}

#pragma mark - tableView
- (void)createTableView {
    
    self.tableView = [[TableViewOfContact alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    
    // 注册cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];
    
    
    
    
    
}

#pragma mark - tableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.arrOfkeys.count;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSString *key = [self.arrOfkeys objectAtIndex:section];
    
    NSArray *arr = [self.dicOfNew objectForKey:key];
    
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    
    
    // cell 赋值.
    NSString *key = [self.arrOfkeys objectAtIndex:indexPath.section];
    
    NSArray *arr = [self.dicOfNew objectForKey:key];
    
    ModelOfContact *model = [arr objectAtIndex:indexPath.row];
    
    cell.textLabel.text = model.name;
    cell.detailTextLabel.text = model.phoneNumber;
    
    return cell;
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    return [self.arrOfkeys objectAtIndex:section];
}

- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    
    return self.arrOfkeys;
}

#pragma mark - tableView Delegate



#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
